//
//  HYShadowViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "HYShadowViewController.h"
#import "CJShadowListModel.h"
#import "CJShadowTableViewCell.h"
#import "PKRequestManager.h"
#import "CJShadowFilmTableViewController.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>
@interface HYShadowViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *sourceArray;
@property (nonatomic, strong) UITableView *shadowTableView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) MBProgressHUD *hud;


@end

@implementation HYShadowViewController

//影单
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"影单";
    
    _page = 1;
    self.view.backgroundColor = kRandomClolor;
    _sourceArray = [NSMutableArray array];


    [self requestData];
    [self hudMore];
    [self addShadowTableView];
    
    
    //刷新
    self.shadowTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (_page > 2) {
            _page -= 1;
            [self requestData];
        } else if (_page == 1) {
            [self requestData];
        }
    }];
    
    self.shadowTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _page += 1;
        [self requestData];
        
    }];
}


//菊花
-(void)hudMore
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.labelText = @"休息一下";
    _hud.minSize = CGSizeMake(100, 100);
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    [_hud show:YES];
}


- (void)requestData {
    
    NSString *urlStr = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=collection&op=recommend&androidflag=1&appfrom=ios&iosversion=3.13&page=%ld",_page];
    
    [PKRequestManager requestWithUrlString:urlStr parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        [_hud hide:YES];

        if (_page == 1) {
            [_sourceArray removeAllObjects];
        }
        
        
        NSArray *array = dataDic[@"data"][@"list"];
        
        for (NSDictionary *dic in array) {
            CJShadowListModel *model = [[CJShadowListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_sourceArray addObject:model];
        }
        [self.shadowTableView.mj_footer endRefreshing];
        [self.shadowTableView.mj_header endRefreshing];
        [self.shadowTableView reloadData];
    } error:^(NSError *error) {
        
    }];
 
}


- (void)addShadowTableView {
    
    _shadowTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarHeight) style:(UITableViewStylePlain)];
    
    _shadowTableView.dataSource = self;
    _shadowTableView.delegate = self;
    
    [self.view addSubview:_shadowTableView];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _sourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *idenfitider = @"CJShadowTableViewCell";
    CJShadowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfitider];
    if (cell == nil) {
        cell = [[CJShadowTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:idenfitider];
    }
    
    cell.model = _sourceArray[indexPath.row];
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJShadowFilmTableViewController *shadowFilmVC = [[CJShadowFilmTableViewController alloc] init];
    
    CJShadowListModel *model = _sourceArray[indexPath.row];
    shadowFilmVC.ctid = model.ctid;
    
    [self.navigationController pushViewController:shadowFilmVC animated:YES];
    
    
    
}







- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat height = [CJShadowTableViewCell cellOfContentLabelHeight:_sourceArray[indexPath.row]];
    
    return height + 90 + (kScreenWidth / 5 * 3);
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
